package com.guo;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;

/**
 * @author gonghuaiguo
 */
public class ExcelWriteTest {

    public static final String PATH = "D:\\javaLearn\\springboot-learn\\guo-poi\\";

    @Test
    public void testWrite03() {
        // 创建一个工作簿
        HSSFWorkbook sheets = new HSSFWorkbook();
        // 创建一个工作表
        HSSFSheet sheet = sheets.createSheet("学习excel工作表");
        // 创建第一个行(1,1)
        HSSFRow row1 = sheet.createRow(0);
        // 创建一个单元格
        HSSFCell cell11 = row1.createCell(0);
        cell11.setCellValue("excel分类");
        HSSFCell cell12 = row1.createCell(1);
        cell12.setCellValue("学习进度");
        HSSFCell cell13 = row1.createCell(2);
        cell13.setCellValue("统计时间");
        // 创建第二行
        HSSFRow row2 = sheet.createRow(1);
        HSSFCell cell21 = row2.createCell(0);
        cell21.setCellValue("EXCEL1");
        HSSFCell cell22 = row2.createCell(1);
        cell22.setCellValue(0.7);
        HSSFCell cell23 = row2.createCell(2);
        cell23.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        HSSFRow row3 = sheet.createRow(2);
        HSSFCell cell31 = row3.createCell(0);
        cell31.setCellValue("EXCEL2");
        HSSFCell cell32 = row3.createCell(1);
        cell32.setCellValue(0.3);
        HSSFCell cell33 = row3.createCell(2);
        cell33.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        try (FileOutputStream fileOutputStream = new FileOutputStream(PATH + "学习excel情况表03.xls")) {
            sheets.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("学习Excel情况表 生成完毕！");

    }

    @Test
    public void testWrite07() {
        // 创建一个工作簿
        Workbook sheets = new XSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = sheets.createSheet("学习excel工作表");
        // 创建第一个行(1,1)
        Row row1 = sheet.createRow(0);
        // 创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("excel分类");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("学习进度");
        Cell cell13 = row1.createCell(2);
        cell13.setCellValue("统计时间");
        // 创建第二行
        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("EXCEL1");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(0.7);
        Cell cell23 = row2.createCell(2);
        cell23.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        Row row3 = sheet.createRow(2);
        Cell cell31 = row3.createCell(0);
        cell31.setCellValue("EXCEL2");
        Cell cell32 = row3.createCell(1);
        cell32.setCellValue(0.3);
        Cell cell33 = row3.createCell(2);
        cell33.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        try (FileOutputStream fileOutputStream = new FileOutputStream(PATH + "学习excel情况表07.xlsx")) {
            sheets.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("学习Excel情况表 生成完毕！");

    }

    @Test
    public void testWriteToBigData03() {
        // 开始时间
        long start = System.currentTimeMillis();
        // 创建工作簿
        Workbook workbook = new HSSFWorkbook();
        // 创建表
        Sheet sheet = workbook.createSheet();
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue("艾滋病的初期症状包括发热、咽炎、淋巴结肿大、皮疹和疲劳，这些症状可能表明潜在的健康问题，建议及时就医进行相关检测和评估。");
            }
        }

        try (FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据量写入xls表String.xls")) {
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时：time=" + (double) (end - start) / 1000+"秒");

    }

    @Test
    public void testWriteToBigData07() {
        // 开始时间
        long start = System.currentTimeMillis();
        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建表
        Sheet sheet = workbook.createSheet();
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue("艾滋病的初期症状包括发热、咽炎、淋巴结肿大、皮疹和疲劳，这些症状可能表明潜在的健康问题，建议及时就医进行相关检测和评估。");
            }
        }

        try (FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据量写入xls表String.xlsx")) {
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时：time=" + (double) (end - start) / 1000+"秒");

    }

    @Test
    public void testWriteToBigData07S() {
        // 开始时间
        long start = System.currentTimeMillis();
        // 创建工作簿
        Workbook workbook = new SXSSFWorkbook();
        // 创建表
        Sheet sheet = workbook.createSheet();
        for (int rowNum = 0; rowNum < 65536; rowNum++) {
            Row row = sheet.createRow(rowNum);
            for (int cellNum = 0; cellNum < 10; cellNum++) {
                Cell cell = row.createCell(cellNum);
                cell.setCellValue("艾滋病的初期症状包括发热、咽炎、淋巴结肿大、皮疹和疲劳，这些症状可能表明潜在的健康问题，建议及时就医进行相关检测和评估。");
            }
        }

        try (FileOutputStream fileOutputStream = new FileOutputStream(PATH + "大数据量写入xls表S.xlsx")) {
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时：time=" + (double) (end - start) / 1000+"秒");

    }
}
